package com.iss.okair.controller.baseinfo;

import com.iss.common.Message;
import com.iss.okair.db.entity.Sysuser;
import com.iss.okair.db.entity.SysuserRole;
import com.iss.okair.service.baseinfo.SysuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.List;

@RestController
@RequestMapping("logic/baseInfo/sysUser")
@SessionAttributes("currentUser")
public class SysuserController {
    @Autowired //自动实例化
    private SysuserService service;
    //登录的接口
    @RequestMapping("/login")
    public Message login(Sysuser user,ModelMap model){
        //1.结果容器
        Message msg = new Message();
        //2.调用service中的方法进行登陆查询，并且接收返回的结果
        user = service.login(user);
        //3.根据返回的结构，设置结果容器中的数据
        if(user != null){
            msg.setError(false);
            msg.setMsg("/index");
            model.addAttribute("currentUser",user);
        }else{
             msg.setMsg("/Login");
        }
        //4.将结果容器抛出
        return msg;
    }
    @RequestMapping("/currentUser")
   public Sysuser currentUser(ModelMap model){
        return (Sysuser)model.get("currentUser");
   }
   @RequestMapping("/roleList")
    public List<SysuserRole> roleList(){
        return service.roleList();
   }
   @RequestMapping("/find")
    public List<Sysuser> find (Sysuser example){
        return service.findByExample(example);
   }
}
